#include <QSettings>
#include <QMessageBox>
#include <QLibrary>
#include <QPushButton>

#include "hierarchyview.h"

HierarchyView::HierarchyView(QWidget *parent) : QDockWidget(parent)//, ui(new Ui::HierarchyView)
{
    setupUi(this);
    //treeView->setModel(new TreeModel("lineloop", "linename", this));
    connect(btnRefresh, SIGNAL(clicked()), this, SLOT(loadHierarchy()));
    //treeView->setColumnHidden(1, true);
    //treeView->columnViewportPosition
}

HierarchyView::~HierarchyView()
{

}

void HierarchyView::loadHierarchy()
{
    //if(model) delete model;
    model = new TreeModel("lineloop", "linename", this);
    treeView->setModel(model);
    treeView->setColumnWidth(0, treeView->width()*.8);
    treeView->hideColumn(1);
    int btnSize = 20;
    for(int i=0;i<model->rowCount(); i++)
    {
        QModelIndex idx = model->index(i, 0);
        for( int j=0; j<model->rowCount(model->index(i, 0)); j++ )
        {
            QFrame *buttonsFrame = new QFrame();
            buttonsFrame->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
            buttonsFrame->setMaximumSize(QSize(3*(btnSize+1), btnSize));
            QHBoxLayout *layout = new QHBoxLayout(buttonsFrame);
            layout->setContentsMargins(0, 0, 0, 0);
            layout->setSpacing(1);
            QSize sz(btnSize, btnSize);
            //Профиль
            QPushButton *btnProfile = new QPushButton(QIcon( ":/icons/mActionExpandTree.png" ), "");
            btnProfile->setIconSize(sz);
            btnProfile->setMaximumSize(sz);
            layout->addWidget(btnProfile);
            //Зум
            QPushButton *btnZoom = new QPushButton(QIcon( ":icons/mActionZoomToSelected.png" ), "");
            btnZoom->setIconSize(sz);
            btnZoom->setMaximumSize(sz);
            layout->addWidget(btnZoom);
            //Паспорт
            QPushButton *btnCard = new QPushButton(QIcon( ":icons/mActionUnselectAttributes.png" ), "" );
            btnCard->setIconSize(sz);
            btnCard->setMaximumSize(sz);
            layout->addWidget(btnCard);
            //           
            connect(btnProfile, SIGNAL(clicked()), this, SLOT(nodeProfile()));
            connect(btnZoom, SIGNAL(clicked()), this, SLOT(nodeZoom()));
            connect(btnCard, SIGNAL(clicked()), this, SLOT(nodePassport()));
            treeView->setColumnWidth(2, (btnSize+1)*3);
            treeView->setIndexWidget(model->index(j, 2, idx), buttonsFrame);            
        }
    }
}

void HierarchyView::getNodeAtCursor()
{

}

void HierarchyView::nodeProfile()
{
    getNodeAtCursor();
}

void HierarchyView::nodeZoom()
{
    getNodeAtCursor();
}

void HierarchyView::nodePassport()
{
    getNodeAtCursor();
}
